<?php
/**
 * Created by PhpStorm.
 * User: HBB-ice
 * Date: 2015/7/28
 * Time: 15:16
 */

namespace Apps\Manage\Controllers;


use Apps\Common\Models\UserOrder;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;

class OrderController extends BaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->setMainNav('order');
    }

    public function indexAction()
    {
        $this->response->redirect('order/list');
    }

    public function listAction()
    {
        $this->setLeftNav('list');
        $req = $this->request;
        $page = intval($req->getQuery('page', null, 1));
        $page = $page > 0 ? $page : 1;
        $limit = $this->pageNavLimit;
        $offset = $limit * ($page - 1);

        $filterId = intval($req->getQuery('filterId', null, -10000));
        $filterUserId = intval($req->getQuery('filterUserId', null, -10000));
        $filterState = intval($req->getQuery('filterState', null, -10000));
        $filterDtStart = $req->getQuery('filterDtStart', null, '');
        $filterDtEnd = $req->getQuery('filterDtEnd', null, '');

        $columns = [
            'order_id',
            'order_long_id',
            'ub.user_id',
            'user_nickname',
            'user_cover',
            'order_mobile',
            'order_type',
            'order_origin',
            'order_money',
            'order_freight',
            'order_remake',
            'order_state',
            'order_pay_type',
            'order_pay_state',
            'order_addtime'
        ];
        $where = [];
        $bind = [];

        if($filterId > 0) {
            $where[] = 'order_long_id = :id:';
            $bind['id'] = $filterId;
        }
        if($filterUserId > 0) {
            $where[] = 'Apps\Common\Models\UserOrder.user_id = :uid:';
            $bind['uid'] = $filterUserId;
        }
        if($filterState > -10000) {
            if($filterState == 2) {
                $where[] = 'order_pay_state = 2';
            } else {
                $where[] = 'order_state = :state:';
                $bind['state'] = $filterState;
            }
        }
        if(!empty($filterDtStart)) {
            $where[] = 'order_addtime >= :dtStart:';
            $bind['dtStart'] = $filterDtStart . ' 00:00:00';
        }
        if(!empty($filterDtEnd)) {
            $where[] = 'order_addtime <= :dtEnd:';
            $bind['dtEnd'] = $filterDtEnd . ' 23:59:59';
        }

        $whereStr = implode(' AND ', $where);
        $query = UserOrder::query()
            ->columns($columns)
            ->leftJoin('Apps\Common\Models\UserBase', 'ub.user_id = Apps\Common\Models\UserOrder.user_id', 'ub');

        if(count($where) > 0) {
            $query = $query->where($whereStr)->bind($bind);
        }
        $data = $query
            ->orderBy('order_id DESC')
            ->limit($limit, $offset)
            ->execute();
        $total = UserOrder::count([
            'conditions' => $whereStr,
            'bind' => $bind,
        ]);

        $this->view->setVar('data', $data);
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        $this->view->setVar('total', $total);

        $this->view->setVar('filterId', $filterId);
        $this->view->setVar('filterUserId', $filterUserId);
        $this->view->setVar('filterState', $filterState);
        $this->view->setVar('filterDtStart', $filterDtStart);
        $this->view->setVar('filterDtEnd', $filterDtEnd);
    }

    public function setRefundAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = -10;

            $info = UserOrder::findFirst('order_id=' . $id);
            if($info) {
                $info->update(['order_state' => $state, 'order_pay_state' => $state]);
            }
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }
}